package cloudContact.DAO;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.awt.image.Raster;
import java.awt.image.WritableRaster;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Hashtable;

import javax.imageio.ImageIO;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

public class FacebookContact {

	public static void main(String[] args) throws IOException {
		FacebookContact contact = new FacebookContact("sompops");
		contact.getImage();
	}

	private JsonObject contactObject;
	private String userId;

	public FacebookContact(String contactId) throws IOException {
		userId = contactId;

		URL theURL = new URL("https://graph.facebook.com/" + contactId);
		BufferedReader in = new BufferedReader(new InputStreamReader(
				theURL.openStream()));

		StringBuilder reply = new StringBuilder();
		String line = "";
		while ((line = in.readLine()) != null) {
			reply.append(line);
		}

		JsonParser parser = new JsonParser();
		contactObject = parser.parse(reply.toString()).getAsJsonObject();
	}

	private String id = null;

	public String getId() {
		if (id == null) {
			JsonElement element = contactObject.get("id");
			if (element == null)
				return "";
			id = element.getAsString();
			;
		}
		return id;
	}

	private String firstName = null;

	public String getFirstName() {
		if (firstName == null) {
			JsonElement element = contactObject.get("first_name");
			if (element == null)
				return "";
			firstName = element.getAsString();
		}
		return firstName;

	}

	private String lastName = null;

	public String getLastName() {
		if (lastName == null) {
			JsonElement element = contactObject.get("last_name");
			if (element == null)
				return "";
			lastName = element.getAsString();
		}
		return lastName;
	}

	private String fullName = null;

	public String getFullName() {
		if (fullName == null) {
			JsonElement element = contactObject.get("name");
			if (element == null)
				return "";
			fullName = element.getAsString();
		}
		return fullName;
	}

	public byte[] getImage() {
		// TODO Auto-generated method stub
		URL theURL;
		try {
			System.out.println("Start to get image from facebook...");
			theURL = new URL("https://graph.facebook.com/" + userId
					+ "/picture?type=large");
			BufferedImage image = ImageIO.read(theURL);

			ByteArrayOutputStream baos = new ByteArrayOutputStream();

			ImageIO.write(image, "jpg", baos);

			System.out.println("Finish get image from facebook...");
			return baos.toByteArray();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

}
